package com.xxyf.service;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.xxyf.entity.Username;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author 小小怡飞
 * @Date 2022/12/3 14:48
 * @Version JDK 8
 */
@Getter
@NoArgsConstructor
@AllArgsConstructor
public class LogUserDetaImp implements UserDetails {
    private Username user;
    private List<String> permissions;
    private List<GrantedAuthority> authorities;
    public LogUserDetaImp(Username user,List<String> permissions){
        this.user=user;
        this.permissions=permissions;
    }
    @JsonIgnore
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        if (authorities!=null){
            return authorities;
        }
//        获取权限信息
        if (permissions!=null){
            return authorities = permissions.stream().map(SimpleGrantedAuthority::new).collect(Collectors.toList());
        }else {
            return null;
        }


    }
    @JsonIgnore
    @Override
    public String getPassword() {
        return user.getPassword();
    }
    @JsonIgnore
    @Override
    public String getUsername() {
        return user.getAccount();
    }
    @JsonIgnore
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }
    @JsonIgnore
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }
    @JsonIgnore
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }
    @JsonIgnore
    @Override
    public boolean isEnabled() {
        return true;
    }


}
